################################
# REPLICATION: FIELD SURVEY ####
# Handi Li, June 2025    
################################

## Load Packages ####

packages <- c("readr","tidyverse","ggplot2", "fixest","kableExtra")

for (pkg in packages) {
  if (!require(pkg, character.only = TRUE)) {
    install.packages(pkg)
    library(pkg, character.only = TRUE)
  }
}

## Load Data ####
d.field_all <- read_csv("/data_survey_all.csv", locale = locale(encoding = "UTF-8"))
d.field_all$age <- 2021 - d.field_all$birth

## Balance ####

names <- c("gender","age","rural","econ_perception","education",
           "income","know_official","know_lawyer",
           "socpol_news","know_ogi","use_ogi","trust_ogi","dispute_experience",
           "choose_legal","choose_political","choose_protest","important_ogi",
           "important_connection","able_connection",
           "attention","survey_times",
           "group","positive_treat")
samp.bal <- d.field_all[names]
samp.bal <- data.frame(na.omit(samp.bal))

source("/balance_test.r")

kable(balance(samp.bal[1:21], samp.bal$positive_treat, names[1:21]), "rst")

## Results ####

# Rescale answers

d.field_all[,c(12:13,15,17:18,20:23,27:30,35:42)] <- lapply(d.field_all[,c(12:13,15,17:18,20:23,27:30,35:42)], function(x)(x-1)/4)

# Create outcome variables for DiD

d.field_all$diff_legal <- d.field_all$legal - d.field_all$choose_legal
d.field_all$diff_political <- d.field_all$political - d.field_all$choose_political

#### Result for Table 5 and Table 6 ####

m.take <- feols(take ~ positive_treat + factor(block) + factor(province), 
                data = d.field_all, cluster = ~ block)

m.legal <- feols(legal ~ positive_treat, 
                 data = d.field_all, cluster = ~ block)

m.political <- feols(political ~ positive_treat + factor(block) + factor(province), 
                     data = d.field_all, cluster = ~ block)

m.institution.direct <- feols(protest_later ~ positive_treat + factor(block) + factor(province), 
                              data = d.field_all, cluster = ~ block)

m.institution.rank <- feols(institution_first1 ~ positive_treat + factor(block) + factor(province), 
                            data = d.field_all, cluster = ~ block)

m.rank_legal <- feols(rank_legal ~ positive_treat + factor(block), 
                      data = d.field_all, cluster = ~ block)

m.rank_political <- feols(rank_political ~ positive_treat + factor(block) + factor(province), 
                          data = d.field_all, cluster = ~ block)

m.fair_legal <- feols(fair_legal ~ positive_treat + factor(block) + factor(province), 
                      data = d.field_all, cluster = ~ block)

m.fair_political <- feols(fair_political ~ positive_treat + factor(block) + factor(province), 
                          data = d.field_all, cluster = ~ block)

#### Result for Table 5 and Table 6 with control variables ####

m.take.c <- feols(take ~ positive_treat + gender + age + rural + education + income + econ_perception +
                    know_official + know_lawyer + socpol_news + know_ogi + use_ogi + trust_ogi + dispute_experience +
                    important_ogi + important_connection + able_connection + attention + factor(block) + factor(province), 
                  data = d.field_all, cluster = ~ block)

m.legal.c <- feols(legal ~ positive_treat + gender + age + rural + education + income + econ_perception +
                     know_official + know_lawyer + socpol_news + know_ogi + use_ogi + trust_ogi + dispute_experience +
                     important_ogi + important_connection + able_connection + attention + factor(block) + factor(province), 
                   data = d.field_all, cluster = ~ block)

m.political.c <- feols(political ~ positive_treat + gender + age + rural + education + income + econ_perception +
                         know_official + know_lawyer + socpol_news + know_ogi + use_ogi + trust_ogi + dispute_experience +
                         important_ogi + important_connection + able_connection + attention + factor(block) + factor(province), 
                       data = d.field_all, cluster = ~ block)

m.institution.direct.c <- feols(protest_later ~ positive_treat + gender + age + rural + education + income + econ_perception +
                                  know_official + know_lawyer + socpol_news + know_ogi + use_ogi + trust_ogi + dispute_experience +
                                  important_ogi + important_connection + able_connection + attention + factor(block) + factor(province), 
                                data = d.field_all, cluster = ~ block)

m.institution.rank.c <- feols(institution_first1 ~ positive_treat + gender + age + rural + education + income + econ_perception +
                                know_official + know_lawyer + socpol_news + know_ogi + use_ogi + trust_ogi + dispute_experience +
                                important_ogi + important_connection + able_connection + attention + factor(block) + factor(province), 
                              data = d.field_all, cluster = ~ block)

m.rank_legal.c <- feols(rank_legal ~ positive_treat + gender + age + rural + education + income + econ_perception +
                          know_official + know_lawyer + socpol_news + know_ogi + use_ogi + trust_ogi + dispute_experience +
                          important_ogi + important_connection + able_connection + attention + factor(block) + factor(province), 
                        data = d.field_all, cluster = ~ block)

m.rank_political.c <- feols(rank_political ~ positive_treat + gender + age + rural + education + income + econ_perception +
                              know_official + know_lawyer + socpol_news + know_ogi + use_ogi + trust_ogi + dispute_experience +
                              important_ogi + important_connection + able_connection + attention + factor(block) + factor(province), 
                            data = d.field_all, cluster = ~ block)

m.fair_legal.c <- feols(fair_legal ~ positive_treat + gender + age + rural + education + income + econ_perception +
                          know_official + know_lawyer + socpol_news + know_ogi + use_ogi + trust_ogi + dispute_experience +
                          important_ogi + important_connection + able_connection + attention + factor(block) + factor(province), 
                        data = d.field_all, cluster = ~ block)

m.fair_political.c <- feols(fair_political ~ positive_treat + gender + age + rural + education + income + econ_perception +
                              know_official + know_lawyer + socpol_news + know_ogi + use_ogi + trust_ogi + dispute_experience +
                              important_ogi + important_connection + able_connection + attention + factor(block) + factor(province), 
                            data = d.field_all, cluster = ~ block)

#### Result for Table F.3 ####

m.cost <- feols(lowcost_legal ~ positive_treat + rural + factor(block) + factor(province), 
                data = d.field_all, cluster = ~ block)
m.cost.c <- feols(lowcost_legal ~ positive_treat + gender + age + rural + education + income + econ_perception +
                    know_official + know_lawyer + socpol_news + know_ogi + use_ogi + trust_ogi + dispute_experience +
                    important_ogi + important_connection + able_connection + attention + factor(block) + factor(province), 
                  data = d.field_all, cluster = ~ block)

m.collective <- feols(collective_action ~ positive_treat + rural + factor(block) + factor(province), 
                      data = d.field_all, cluster = ~ block)
m.collective.c <- feols(collective_action ~ positive_treat + gender + age + rural + education + income + econ_perception +
                          know_official + know_lawyer + socpol_news + know_ogi + use_ogi + trust_ogi + dispute_experience +
                          important_ogi + important_connection + able_connection + attention + factor(block) + factor(province), 
                        data = d.field_all, cluster = ~ block)

m.social_desire1 <- feols(post_criticism ~ positive_treat + rural + factor(block) + factor(province), 
                          data = d.field_all, cluster = ~ block)
m.social_desire1.c <- feols(post_criticism ~ positive_treat + gender + age + rural + education + income + econ_perception +
                              know_official + know_lawyer + socpol_news + know_ogi + use_ogi + trust_ogi + dispute_experience +
                              important_ogi + important_connection + able_connection + attention + factor(block) + factor(province), 
                            data = d.field_all, cluster = ~ block)

m.social_desire2 <- feols(noanswer ~ positive_treat + rural + factor(block) + factor(province), 
                          data = d.field_all, cluster = ~ block)
m.social_desire2.c <- feols(noanswer ~ positive_treat + gender + age + rural + education + income + econ_perception +
                              know_official + know_lawyer + socpol_news + know_ogi + use_ogi + trust_ogi + dispute_experience +
                              important_ogi + important_connection + able_connection + attention + factor(block) + factor(province), 
                            data = d.field_all, cluster = ~ block)

## Attrition and Bounds (Tables F.1, F.2) ####

attrition_rate_legal <- nrow(subset(d.field_all,is.na(d.field_all$legal)))/nrow(d.field_all)
attrition_rate_political <- nrow(subset(d.field_all,is.na(d.field_all$political)))/nrow(d.field_all)
attrition_rate_direct <- nrow(subset(d.field_all,is.na(d.field_all$protest_later)))/nrow(d.field_all)
attrition_rate_rank_legal <- nrow(subset(d.field_all,is.na(d.field_all$rank_legal)))/nrow(d.field_all) # rate = 7.6%
attrition_rate_rank <- nrow(subset(d.field_all,is.na(d.field_all$institution_first1)))/nrow(d.field_all) # rate = 10.6%
attrition_rate_fair1 <- nrow(subset(d.field_all,is.na(d.field_all$fair_legal)))/nrow(d.field_all)
attrition_rate_fair2 <- nrow(subset(d.field_all,is.na(d.field_all$fair_political)))/nrow(d.field_all)
attrition_rate_confidence <- nrow(subset(d.field_all,is.na(d.field_all$confidence_institution)))/nrow(d.field_all)
attrition_rate_information <- nrow(subset(d.field_all,is.na(d.field_all$information_institution)))/nrow(d.field_all)
attrition_rate_take <- nrow(subset(d.field_all,is.na(d.field_all$take)))/nrow(d.field_all)
attrition_rate_lowcost <- nrow(subset(d.field_all,is.na(d.field_all$lowcost_legal)))/nrow(d.field_all)
attrition_rate_collective <- nrow(subset(d.field_all,is.na(d.field_all$collective_action)))/nrow(d.field_all)

# Apply Lee Bounds

d.field_check <- d.field_all

pi1 <- nrow(d.field_check[!is.na(d.field_check$rank_legal) & d.field_check$positive_treat == 1,])/nrow(d.field_check[d.field_check$positive_treat == 1,])
pi0 <- nrow(d.field_check[!is.na(d.field_check$rank_legal) & d.field_check$positive_treat == 0,])/nrow(d.field_check[d.field_check$positive_treat == 0,])
q <- (pi1 - pi0)/pi1

nrow(d.field_check[d.field_check$positive_treat == 1,])*q

set.seed(23)
d.field_check_upp <- d.field_check[-sample(which(d.field_check$rank_legal==1 & d.field_check$positive_treat == 1), 3),]
d.field_check_low <- d.field_check[-sample(which(d.field_check$rank_legal==4 & d.field_check$positive_treat == 1), 3),]

summary(feols(rank_legal ~ positive_treat + factor(block), 
              data = d.field_check_upp, cluster = ~ block))

summary(feols(rank_legal ~ positive_treat + gender + age + rural + education + income + econ_perception +
                know_official + know_lawyer + socpol_news + know_ogi + use_ogi + trust_ogi + dispute_experience +
                important_ogi + important_connection + able_connection + attention + factor(block) + factor(province), 
              data = d.field_check_upp, cluster = ~ block))#

summary(feols(rank_legal ~ positive_treat + factor(block), 
              data = d.field_check_low, cluster = ~ block))#

summary(feols(rank_legal ~ positive_treat + gender + age + rural + education + income + econ_perception +
                know_official + know_lawyer + socpol_news + know_ogi + use_ogi + trust_ogi + dispute_experience +
                important_ogi + important_connection + able_connection + attention + factor(block) + factor(province), 
              data = d.field_check_low, cluster = ~ block))#


pi1 <- nrow(d.field_check[!is.na(d.field_check$institution_first1) & d.field_check$positive_treat == 1,])/nrow(d.field_check[d.field_check$positive_treat == 1,])
pi0 <- nrow(d.field_check[!is.na(d.field_check$institution_first1) & d.field_check$positive_treat == 0,])/nrow(d.field_check[d.field_check$positive_treat == 0,])
q <- (pi0 - pi1)/pi0

nrow(d.field_check[d.field_check$positive_treat == 0,])*q

set.seed(24)
d.field_check_upp <- d.field_check[-sample(which(d.field_check$institution_first1==0 & d.field_check$positive_treat == 0), 3),]
d.field_check_low <- d.field_check[-sample(which(d.field_check$institution_first1==1 & d.field_check$positive_treat == 0), 3),]

summary(feols(institution_first1 ~ positive_treat + factor(block), 
              data = d.field_check_upp, cluster = ~ block))

summary(feols(institution_first1 ~ positive_treat + gender + age + rural + education + income + econ_perception +
                know_official + know_lawyer + socpol_news + know_ogi + use_ogi + trust_ogi + dispute_experience +
                important_ogi + important_connection + able_connection + attention + factor(block) + factor(province), 
              data = d.field_check_upp, cluster = ~ block))

summary(feols(institution_first1 ~ positive_treat + factor(block), 
              data = d.field_check_low, cluster = ~ block))

summary(feols(institution_first1 ~ positive_treat + gender + age + rural + education + income + econ_perception +
                know_official + know_lawyer + socpol_news + know_ogi + use_ogi + trust_ogi + dispute_experience +
                important_ogi + important_connection + able_connection + attention + factor(block) + factor(province), 
              data = d.field_check_low, cluster = ~ block))
